﻿#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QDebug>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include "Utils_Global.h"

class QTCREATOR_UTILS_EXPORT Logger : public QThread
{
    Q_OBJECT
public:
    virtual ~Logger();

    static Logger *instance();

    static void messageWrapper(QtMsgType type, const QMessageLogContext &context, const QString &msg);
    static void setLogSavePath(const QString &path);
    static void write(QtMsgType type, const QString &fileName, const QString &log, const QString &msg);

    /**
     * @brief exitThread
     */
    void exitThread();

signals:
    /* 当产生的日志时，此信号被发送. 建议UI的日志界面应该绑定此信号 */
    void newMessage(int type, const QString &fileName, const QString &text);
    void sigMessage(const QString &text);

    /* 当产生的日志类型为Critical时，此信号被发送. 建议UI主界面绑定此信号,当严重错误发生时，弹框告知用户。 */
    void critical(const QString &text);

protected:
    virtual void run() override;

private:
    explicit Logger(QObject *parent = nullptr);
    void add(const QString &text);

    std::atomic_bool m_enable{true};
    QStringList m_waitBuffer;
    QWaitCondition m_hasTask;
    QMutex m_mutex;

    static QString m_logPath;
};

#endif // LOGGER_H
